import org.jetbrains.annotations.NotNull;

import java.util.Iterator;
import java.util.function.Consumer;

public class DoubleLinkedListSentinel implements Iterable<Integer>{

    /**
     * {@code head}头节点<br>
     * {@code tail}尾节点
     */
    private final Node head;
    private final Node tail;

    /**
     * 构造函数<br>
     * 同时使{@code head}头节点和{@code tail}尾节点相连
     */
    public DoubleLinkedListSentinel() {
        head = new Node(null,666,null);
        tail = new Node(null,888,null);
        head.next = tail;
        tail.prev = head;
    }


    /**
     * 内部类
     */
    private static class Node {
        Node prev;
        int value;
        Node next;

        public Node(Node prev, int value, Node next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }
    }


    /**
     * 索引异常情况处理
     * @param index 异常索引
     * @return 异常信息
     */
    private IndexOutOfBoundsException illegalIndex(int index) {
        throw new IndexOutOfBoundsException(String.format("index [%d] 不合法%n", index));
    }


    /**
     * 根据索引，找出是否存在该位置节点
     * @param index 索引
     * @return 找到的节点，无则返回{@code null }
     */
    private Node findNode(int index) {
        int i = -1;
        Node curr;
        for (curr = head; curr != tail; curr = curr.next, i++) {
            if (index == i) {
                return curr;
            }
        }
        return null;
    }


    /**
     * 根据索引，在双向链表里插入新节点
     * @param index 索引
     * @param value 新节点值
     */
    public void insert(int index, int value) {
        Node prev = findNode(index - 1);
        if (prev == null) {
            throw illegalIndex(index);
        }
        Node next = prev.next;
        Node inserted = new Node(prev,value,next);
        prev.next = inserted;
        next.prev = inserted;
    }


    /**
     * 尾插法
     * @param value 该节点值
     */
    public void addLast(int value) {
        Node prev = tail.prev;
        Node added = new Node(prev,value,tail);
        prev.next = added;
        tail.prev = added;
    }
    
    /**
     * 头插法
     * @param value 该节点值
     */
    public void addFirst(int value) {
        insert(0,value);
    }


    /**
     * 根据索引删除节点
     * @param index 索引
     */
    public void remove(int index) {
        Node prev = findNode(index - 1);
        if (prev == null) {
            throw illegalIndex(index);
        }
        Node removed = prev.next;
        if (removed == tail) {
            throw illegalIndex(index);
        }
        Node next = removed.next;
        prev.next = next;
        next.prev = prev;
    }

    /**
     * 头删法
     */
    public void removeFirst() {
        remove(0);
    }

    /**
     * 尾删法
     */
    public void removeLast() {
        Node removed = tail.prev;
        if (removed == head) {
            throw illegalIndex(0);
        }
        Node prev = removed.prev;
        prev.next = tail;
        tail.prev = prev;
    }

    /**
     * 根据索引取得当前节点值
     * @param index
     */
    public int get(int index) {
        Node curr = findNode(index);
        if (curr == null) {
            throw illegalIndex(index);
        }
        return curr.value;
    }


    /**
     * for循环
     * @param consumer
     */
    public void forLoop(Consumer<Integer> consumer) {
        for (Node curr = this.head.next; curr != tail; curr = curr.next) {
            consumer.accept(curr.value);
        }
    }

    /**
     * 迭代器遍历
     * @return
     */
    @Override
    public @NotNull Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node curr = head.next;
            @Override
            public boolean hasNext() {
                return curr != tail;
            }

            @Override
            public Integer next() {
                int value = curr.value;
                curr = curr.next;
                return value;
            }
        };
    }

}
